package elearning.controller;

import java.io.Serializable;

import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.zk.ui.select.annotation.VariableResolver;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zk.ui.util.Clients;

import elearning.model.CmsUser;
import elearning.service.CmsUserService;
import elearning.util.StringUtil;

@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
public class CmsUserController implements Serializable{
	private static final long serialVersionUID = 1L;

	@WireVariable
	CmsUserService cmsUserService;	
	CmsUser cmsUser;
	
	public CmsUser getCmsUser() {
		return cmsUser;
	}

	public void setCmsUser(CmsUser cmsUser) {
		this.cmsUser = cmsUser;
	}

	@Init 
	public void init(){
		cmsUser = new CmsUser();
	}
	
	@Command 
	public void login(){
		String pass = cmsUser.getPassword();
		CmsUser user = cmsUserService.getByUserName(cmsUser.getUserName());
		if(user != null){
			String hash = StringUtil.getMD5Hash(pass +user.getSalt());
			if(user.getPassword().equals(hash)){
				Clients.showNotification("login successfully");
			}else{
				Clients.alert("username or password incorrect! try it again");
			}
		}else{
			Clients.alert("account is invalid!");
		}
	}
}
